package myExample;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.cas.FSIterator;
import org.apache.uima.collection.CasConsumer_ImplBase;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.resource.ResourceProcessException;

// CAS Consumer

public class WriterCasConsumer extends CasConsumer_ImplBase {

	private File mOutputDir;

	public void initialize() throws ResourceInitializationException {
	    mOutputDir = new File("output");
	    if (!mOutputDir.exists()) {
	      mOutputDir.mkdirs();
	    }
	  }
	
	@Override
	public void processCas(CAS acas) throws ResourceProcessException {
		// TODO Auto-generated method stub
		JCas jcas;
	    try {
	      jcas = acas.getJCas();
	    } catch (CASException e) {
	      throw new ResourceProcessException(e);
	    }

	    // Retrieve the filename of the input file from the CAS
	    FSIterator srcIterator = jcas.getAnnotationIndex(SourceDocument.type).iterator();
	    FSIterator townIterator = jcas.getAnnotationIndex(Town.type).iterator();
	    File outFile = null;
	    if (srcIterator.hasNext()){
	    	SourceDocument fileLoc = (SourceDocument) srcIterator.next();
	        File inFile;
	        String outFileName = "";
	        try {
	        	// save plain text
	        	inFile = new File(new URL(fileLoc.getURI()).getPath());
	        	outFileName = inFile.getName();
	        	outFile = new File(mOutputDir, outFileName);
	        	String text = acas.getDocumentText();
	        	FileOutputStream outputStr = new FileOutputStream(outFile);
	        	BufferedWriter writerOut = new BufferedWriter(new OutputStreamWriter(outputStr, "UTF-8")); 
	        	writerOut.write(text);
	        	writerOut.close();	          
	        	if (townIterator.hasNext()) {	        		
	        	  	// save result
		        	File result = new File(mOutputDir, "result.txt");	        	
		        	FileOutputStream outputStream = new FileOutputStream(result,true);
					BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
					StringBuilder str = new StringBuilder();
					str.append(outFileName + "\r\n");
					while (townIterator.hasNext()) {
		        		Town town = (Town) townIterator.next();
		        		str.append("\t" + town.getName() + "\t" + town.getBegin() + "\t" + town.getEnd() +"\r\n");
		        	}	        	
		        	writer.write(str.toString());		        	
		        	writer.close();
		        }	   
	        } catch (MalformedURLException e1) {
	            // invalid URL, use default processing below
	        	e1.printStackTrace();
	        } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    	
	}
		
}
